#!/bin/bash
#
# Nautilus script -> jd-gui
#
# Add multiple files jd-gui application
#
# Author:  zurab kumsiashvili
# Version: 1.8
#

filesall=$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
if [ -z $filesall ]; then
    zenity --info --text="Please select a file"
else
    which adb
    if [ $? != 0 ]; then
	zenity --info --text="You either do not have adb installed or adb is not in your path"		
    else
	adbdev=`adb devices`
	arr=($adbdev)
	pattern=('List of devices attached device')
	icon=$(cd "$HOME/.gnome2/nautilus-scripts/"; pwd)"/androicon.png"
	
	for i in ${pattern[@]};
	do
	    arr=(${arr[@]/$i/}) 
	done;
	
	if [ ${#arr[@]} -eq 1 ]; then
	    destdev=${arr[*]}
	elif [ ${#arr[@]} -gt 1 ]; then
	    destdev=`zenity --list ${arr[*]} --window-icon=$icon --column=Devices`
	else
	    zenity --info --text="No Device Found"
	fi
	if [ $destdev ]; then
	    list=`adb -s $destdev shell "ls -d */" | tr -d '\r'`
	    destdir=`zenity --list $list --window-icon=$icon --text=Destination  --column=Folder`
	else 
	    exit
	fi
	
	if [ $destdir ]; then	
	    (
		for f in $filesall; 
		do
		    echo "# Sending $f";
		    text=$(adb -s $destdev push $f $destdir 2>&1)
		    notify-send "Transfer Finished Sucsessfully
$f
$text" --urgency=normal --icon=$icon
		    done) | zenity --progress --title="Send To Android" --text="Sending " --pulsate --auto-close
	else
	    exit
		fi
    fi
fi